package everydayGrade.t4;

import java.util.*;

/**
 * @Author: Siantar
 * @Date: 2023-09-09-15:34
 * @Description: 1.0
 */
public class T0207 {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        List<Integer>[] lists = new List[numCourses];
        for (int i = 0; i < numCourses; i++) {
            lists[i] = new ArrayList<>();
        }
        int[] indeg = new int[numCourses];
        for (int[] e : prerequisites) {
            int p = e[0], pre = e[1];
            lists[pre].add(p);
            indeg[p]++;
        }
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < numCourses; i++) {
            if (indeg[i] == 0) {
                queue.offer(i);
            }
        }
        int qn = 0;
        while (!queue.isEmpty()) {
            qn++;
            int pre = queue.poll();
            for (int p : lists[pre]) {
                indeg[p]--;
                if (indeg[p] == 0) {
                    queue.offer(p);
                }
            }
        }
        return qn == numCourses;
    }

}
